<?php

require_once("xml.class.php");
require_once("simple.xml.class.php");

//class IzvodiPEXIM extends xml_yp {
class IzvodiPEXIM extends simplexml {
	
	var $_filename;
	
	var $in = "credit";
	var $out = "debit";
	
	var $status = array(
		"code" => true,
		"message" => ""
	);
	
	function IzvodiPEXIM($fn){
		if (file_exists($fn)) {
			$this->_filename = $fn;
			/*$this->xml_yp($fn);
			$ret = $this->parse();
			if($ret===false){
				$this->status["code"] = false;
				$this->status["message"] = "Struktura XML fajla nije odgovarajuća.";
			}*/
		}else{
			$this->status["code"] = false;
			$this->status["message"] = "Fajl $fn ne postoji.";
		}
	}
	
	function fetch(){
		$data = $this->xml_load_file($this->_filename);
		if (!is_object($data)) {
			die($data);
		}
		if ($data->pmtnotification) {
			$data = $data->pmtnotification;
		}
		$iz = new izvod();
		$iz->broj = $data->stmtnumber;
		$iz->length = count($data->trnlist->stmttrn);
		
		
		if ($iz->length==1) {
			$data->trnlist->stmttrn = array($data->trnlist->stmttrn);
		}
		
		foreach ($data->trnlist->stmttrn as $k=>$v){
			$ok=true;
			$obj = new dummy();
			if ($v->benefit == $this->in) {
				$o = &$iz->in;
			}else if ($v->benefit == $this->out) {
				$o = &$iz->out;
			}else{
				$ok=false;
			}
			if ($ok) {
				$obj->date = datetime2str(substr($v->dtposted,0,strpos($v->dtposted,"T")));
				$obj->name = $this->fixLetters($v->payeeinfo->name);
				$obj->acc =  $this->fixLetters($v->payeeaccountinfo->acctid);
				$obj->bankid =  $this->fixLetters($v->payeeaccountinfo->bankid);
				$obj->bankname =  $this->fixLetters($v->payeeaccountinfo->bankname);
				$obj->value =  $this->fixLetters($v->trnamt);
				$obj->ugovor =  $this->fixLetters($v->refnumber);
				$obj->ugovor1 =  $this->fixLetters($v->payeerefnumber);
				$obj->purpose = $v->purpose;
				$o[] = $obj;
			}
		}
		return $iz;
	}
	/*
	function getStruct(){
		$data = array();
		$data = array_merge($data,$this->get_elements("trnlist",3,array("open")));
		$data = array_merge($data,$this->get_elements("benefit",5));
		$data = array_merge($data,$this->get_elements("stmtnumber",3));
		$data = array_merge($data,$this->get_elements("name",6));
		$data = array_merge($data,$this->get_elements("acctid",6));
		$data = array_merge($data,$this->get_elements("bankid",6));
		$data = array_merge($data,$this->get_elements("bankname",6));
		$data = array_merge($data,$this->get_elements("trnamt",5));
		$data = array_merge($data,$this->get_elements("refnumber",5));
		$data = array_merge($data,$this->get_elements("payeerefnumber",5));
		
		$broj_izvoda = $data["stmtnumber"][0]["value"];
		$ukupno = $data["trnlist"][0]["attributes"]["count"];
		
		$iz = new izvod();
		$iz->broj = $broj_izvoda;
		$iz->length = $ukupno;
		foreach ($data["benefit"] as $k=>$v){
			
			$ok=true;
			$obj = new dummy();
			if ($v["value"] == $this->in) {
				$o = &$iz->in;
			}else if ($v["value"] == $this->out) {
				$o = &$iz->out;
			}else{
				$ok=false;
			}
			if ($ok) {
				$obj->name = $this->fixLetters($data["name"][$k]["value"]);
				$obj->acc =  $this->fixLetters($data["acctid"][$k]["value"]);
				$obj->bankid =  $this->fixLetters($data["bankid"][$k]["value"]);
				$obj->bankname =  $this->fixLetters($data["bankname"][$k]["value"]);
				$obj->value =  $this->fixLetters($data["trnamt"][$k]["value"]);
				$obj->ugovor =  $this->fixLetters($data["refnumber"][$k]["value"]);
				$obj->ugovor1 =  $this->fixLetters($data["payeerefnumber"][$k]["value"]);
				$o[] = $obj;
			}
		}
		return $iz;
	}*/
	function fixLetters($str){
		$mask = array(
			"Ĺˇ" => "š",
			"Ä‘" => "đ",
			"Ĺľ" => "ž",
			"ÄŤ" => "č",
			"Ä‡" => "ć",
			"Ĺ " => "Š",
			"Ä" => "Đ",
			"Ĺ˝" => "Ž",
			"ÄŚ" => "Č",
			"Ä†" => "Ć"
		);
		foreach ($mask as $k=>$v){
			$str = str_replace($k,$v,$str);
		}
		return $str;
	}
}


class izvod{
	var $in;
	var $out;
	function izvod(){
		$this->in = array();
		$this->out = array();
	}
}
class dummy{
	function dummy(){
		
	}
}


?>